Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

186
Views
Use map vs array[0] in React component to render the only result filtered by Graphql variable

Based on a Graphql variable myKey I query one item of the array of objects:

const { data } = useQuery<GqlRes, Args>(GET_ITEMS, {
    variables: {
      myKey,
    },
    errorPolicy: 'all',
  });

I have to render a Link in my React jsx:

          data.items.map((item) => (
            <Link to={item.url}>
              {item.title}
            </Link>
          ))}

Is it better to use .map (which in this case returns only one item) or do I have to use something like:

const myLink = data.items[0] 

And then use the values like:

<Link to={myLink.url}>
 {myLink.title}
</Link> 
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you always expect to get only one item, doing data.items[0] is fine but there is not harm in using map either. Using map will keep your code dynamic and will be able to render multiple links should the returned data contain more than one result.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!